package com.hz.monitoring.module.hardware.controller.admin.monitoring_data;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.common.pojo.CommonResult;
import static com.hz.monitoring.framework.common.pojo.CommonResult.success;

import com.hz.monitoring.framework.excel.core.util.ExcelUtils;

import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hz.monitoring.module.hardware.controller.admin.monitoring_data.vo.*;
import com.hz.monitoring.module.hardware.dal.dataobject.monitoring_data.MonitoringDataDO;
import com.hz.monitoring.module.hardware.convert.monitoring_data.MonitoringDataConvert;
import com.hz.monitoring.module.hardware.service.monitoring_data.MonitoringDataService;

@Api(tags = "管理后台 - 监测数据")
@RestController
@RequestMapping("/hardware/monitoring-data")
@Validated
public class MonitoringDataController {

    @Resource
    private MonitoringDataService monitoringDataService;

    @PostMapping("/create")
    @ApiOperation("创建监测数据")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-data:create')")
    public CommonResult<Long> createMonitoringData(@Valid @RequestBody MonitoringDataCreateReqVO createReqVO) {
        return success(monitoringDataService.createMonitoringData(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新监测数据")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-data:update')")
    public CommonResult<Boolean> updateMonitoringData(@Valid @RequestBody MonitoringDataUpdateReqVO updateReqVO) {
        monitoringDataService.updateMonitoringData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除监测数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-data:delete')")
    public CommonResult<Boolean> deleteMonitoringData(@RequestParam("id") Long id) {
        monitoringDataService.deleteMonitoringData(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得监测数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-data:query')")
    public CommonResult<MonitoringDataRespVO> getMonitoringData(@RequestParam("id") Long id) {
        MonitoringDataDO monitoringData = monitoringDataService.getMonitoringData(id);
        return success(MonitoringDataConvert.convert(monitoringData));
    }

    @GetMapping("/list")
    @ApiOperation("获得监测数据列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-data:query')")
    public CommonResult<List<MonitoringDataRespVO>> getMonitoringDataList(@RequestParam("ids") Collection<Long> ids) {
        List<MonitoringDataDO> list = monitoringDataService.getMonitoringDataList(ids);
        return success(MonitoringDataConvert.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得监测数据分页")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-data:query')")
    public CommonResult<PageResult<MonitoringDataRespVO>> getMonitoringDataPage(@Valid MonitoringDataPageReqVO pageVO) {
        PageResult<MonitoringDataDO> pageResult = monitoringDataService.getMonitoringDataPage(pageVO);
        return success(MonitoringDataConvert.convertPage(pageResult));
    }


    @GetMapping("/statistical")
    @ApiOperation("统计")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-data:query')")
    public CommonResult<List<MonitoringDataStatisticalRespVO>> statisticalMonitoringData(@Valid MonitoringDataStatisticalReqVO cmd) {
        List<MonitoringDataStatisticalRespVO> list = monitoringDataService.statisticalMonitoringData(cmd);
        return success(list);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出监测数据 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-data:export')")
    @OperateLog(type = EXPORT)
    public void exportMonitoringDataExcel(@Valid MonitoringDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MonitoringDataDO> list = monitoringDataService.getMonitoringDataList(exportReqVO);
        // 导出 Excel
        List<MonitoringDataExcelVO> datas = MonitoringDataConvert.convertList02(list);
        ExcelUtils.write(response, "监测数据.xls", "数据", MonitoringDataExcelVO.class, datas);
    }

}
